package com.crscd.ai.controller;

import com.crscd.ai.utils.DateTimeTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 10/9/25 2:59 PM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
public class ToolCallingController {
  @Qualifier("qwen")
  @Autowired
  private ChatModel chatModel;

  @Qualifier("qwenChatClient")
  @Autowired
  private ChatClient chatClient;

  @GetMapping("/toolcall/chat")
  public String chat(@RequestParam(name = "msg", defaultValue = "你是谁，现在几点了") String msg) {
    // 1.工具注册到工具集合里
    ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());
    // 2.将工具集配置进ChatOptions对象
    ChatOptions options = ToolCallingChatOptions.builder().toolCallbacks(tools).build();
    // 3.构建提示词
    Prompt prompt = new Prompt(msg, options);
    // 4.调用大模型
    return chatModel.call(prompt).getResult().getOutput().getText();
  }

  @GetMapping("/toolcall/chat2")
  public Flux<String> chat2(@RequestParam(name = "msg", defaultValue = "你是谁，现在几点了") String msg) {
    ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());
    ChatOptions options = ToolCallingChatOptions.builder().toolCallbacks(tools).build();
    return chatClient.prompt(msg).options(options).tools(new DateTimeTools()).stream().content();
  }
}
